package ext.acae2.cad.client;

import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wt.httpgw.GatewayAuthenticator;
import wt.method.RemoteMethodServer;
import ext.acae2.cad.CADSignHelper;

/**
 * @desription 二维图电子签名客户端
 * @author cnie
 * @date 2013-7-17 下午8:44:01
 */
public class CADSignClient {
	
	private static List<String> signClient(String username,String password,Map<String,Map<String, String>> cadSignMap) throws Exception{
		RemoteMethodServer rms = RemoteMethodServer.getDefault();
		rms.setUserName(username);
		rms.setPassword(password);
		GatewayAuthenticator auth = new GatewayAuthenticator();
		auth.setRemoteUser(username);
		auth.setServer(rms);
		rms.setAuthenticator(auth);
		List<String> errorList= (List<String>) rms.invoke("signCADS", CADSignHelper.class.getName(), null, new Class[]{Map.class}, new Object[]{cadSignMap});
		return errorList;
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println("arguments format:");
		System.out.println("       1.{0}:username");
		System.out.println("       2.{1}:password");
		System.out.println("       3.{2}:cad sign info;format reference:${wt.home}/codebase/ext/acae2/cad/signInfo.xml");

		if (args.length < 3) {
			System.out.println("arguments count is error");
			return;
		}
		Map<String, Map<String, String>> signInfo = CADSignHelper
				.readSignInfoFormXML(args[2].trim());
		if (signInfo == null) {
			System.out.println("(ERROR):cad sign fail");
		} else {
			System.out.println("**********************************************************************");
			String username = args[0].trim();
			String password = args[1].trim();
			List<String> errorList = null;
			try {
				errorList = signClient(username, password,
						signInfo);
				if (errorList != null && errorList.size() == 0) {
					System.out.println("cad sign success");
				} else {
					for(String error:errorList){
						System.out.println(error);
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("system exception;please see the logs");
			}
			System.out.println("**********************************************************************");
		}
	}
}
